﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodType_Manager :  PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/GoodType_Manager.aspx";

        base.OnInit(e);
    }

    #endregion

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("GoodType_Edit.aspx");
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            string id = dr["ID"].ToString();

            Button btnDelete = (Button)e.Item.Cells[7].FindControl("btnDelete");
            Button btnUpGoods = (Button)e.Item.Cells[7].FindControl("btnUpGoods");

            //if (id == "1")
            //{
            //    btnDelete.Enabled = false;
            //}

            btnUpGoods.Attributes.Add("onclick", "return ExcelInput(" + id + ")");               
        }
    }

    protected void g_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {        
        if (e.CommandName == "btnDelete")
        {
            DataTable dt = null;
            long ID = ShoveEL._Convert.StrToLong(e.Item.Cells[0].Text, -1);

            try
            {
                //查询该类别下是否有商品
                dt = DAL.Tables.T_GoodSales.Select("", "GoodID = " + ID, "");
                if (dt != null && dt.Rows.Count > 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this, "该商品档案正在使用，不能删除该档案！");
                    return;
                }
                else
                {
                    int i = DAL.Tables.T_GoodType.Delete("[ID]=" + ID);

                    if (i >= 0)
                    {
                        ShoveEL._Web.JavaScript.Alert(this, "删除成功。");
                        BindData();
                    }
                }
            }
            catch
            {
                ShoveEL._Web.JavaScript.Alert(this, "删除失败。");
            }            
        }
        else if (e.CommandName == "btnUpGoods")
        {
            
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        DataTable dt = DAL.Tables.T_GoodType.Select("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string Path = Server.MapPath("~/UpFiles/Excel/TemplateExcel_Goods.xls");

        bool success = DataTableToExcel.DownloadFile(Page.Request, Page.Response, "Goods.xls", Path, 1024000);

        if (!success)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "下载文件出错！");

        }
    }

}


